<template>
  <div>
    <!-- 返利子项目维护 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>

    <FilterTable ref="table" :columns="columns" :dataSource="tableList" :isSelectColumn="false" :onColumnsChange="columns => (this.columns = columns)">
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" @click="addHandle(1)">新 增</multiuse-button>
        <multiuse-button size="small" type="primary" :click="saveFun" :disabled="tableList.length < 1">保存</multiuse-button>
      </template>
    </FilterTable>
    <BaseDialog :visible.sync="isAdd" :title="'返利子项目维护新增'"  destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <addModel @close="addcloseHandler" :rowData="rowData"  :type="type" />
    </BaseDialog>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { getList, saveSort, getFundsType } from '@/api/omFinance/RebateManage/RebateChilrden/index.js';
import addModel from './add';
export default {
  name: 'RebateChilrden',
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchParams: {},
      rowData:{},
      type:"add",
      isAdd: false,
      tableList: []
    };
  },
  mixins: [dictionary],
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
    this.getAccount();
    this.getTableList();
  },
  created() {},
  components: {
    addModel
  },
  methods: {
    createdFrom() {
      return [
        {
          type: 'SELECT',
          label: '返利类型',
          fieldName: 'accountTypeId',
          placeholder: '请选择',
          itemList: []
        },
        {
          type: 'INPUT',
          label: '返利子项目',
          fieldName: 'rebateSubprojectsName',
          placeholder: '请输入...'
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          width:80,
          dataIndex: 'column-action',
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.edit(props.row)}>
                  编辑
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '返利类型代码',
          dataIndex: 'accountTypeCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '返利类型名称',
          dataIndex: 'accountTypeName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '返利子项目代码',
          dataIndex: 'rebateSubprojectsCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '返利子项目名称',
          dataIndex: 'rebateSubprojectsName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '排序',
          dataIndex: 'num',
          sorter: true,
          filter: true,
          filterType: 'input',
          editable: true,
          defaultEditable: true,
          editType: 'number',
          editRequired: true
        },
        {
          title: '是否有效',
          dataIndex: 'isValid',
          sorter: true,
          filter: true,
          filterItems: this.createDictList('1001'),
          render: props => {
            return <span>{this.createDictText(props.row.isValid, '1001')}</span>;
          },
          filterType: 'input'
        },
        {
          title: '是否可以红冲',
          dataIndex: 'isRed',
          hidden:true,
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems:[{text:'是',value:"1"},{text:'否',value:"0"}],
          editable: true,
          defaultEditable: true,
          editType: 'select',
          editRequired: false,
          editItems:[{text:'是',value:"1"},{text:'否',value:"0"}],
        }
      ];
    },
    async getTableList(data) {
      this.BaseTable.START_LOADING();
      let res = await getList(data);
      if (res.resultCode === 200) {
        this.tableList = res.data || [];
      }
      this.BaseTable.STOP_LOADING();
    },
    async saveFun() {
      let arr = this.tableList.map(x => ({ id: x.id, num: x.num,isRed:x.isRed }));
      let array = [];
      let is = false;
      arr.map(x => {
        if (!x.num) {
          is = 1;
        } else if (!/^[1-9]\d*$/.test(x.num)) {
          is = 2;
        }
      });
      console.log(arr, 'arr');
      if (is == 1) {
        this.$notify.error({
          title: '错误',
          message: '请维护表单内容后进行保存'
        });
        return;
      } else if (is == 2) {
        this.$notify.error({
          title: '错误',
          message: '排序值应填写正整数'
        });
        return;
      }
      await this.$confirm('是否保存?', '业务参数新增', {
        confirmButtonText: '确定',
        cancelButtonText: '关闭',
        type: 'warning'
      }).then(async () => {
        let res = await saveSort(arr);
        if (res.success === true) {
          console.log(res.data);
          this.$notify.success({
            title: '提示信息',
            message: '保存成功'
          });
          this.getTableList(this.fetchParams);
        } else {
          this.$notify.error({
            title: '提示信息',
            message: res.errMsg
          });
        }
      });
    },
    changeHandle(params) {
      this.fetchParams = params;
      this.getTableList(params);
    },
    addHandle() {
      this.type = "add"
      this.isAdd = true;
    },
    edit(row) {//编辑
      this.rowData = row;
      this.type = "edit"
      this.isAdd = true;
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    addcloseHandler(val) {
      if (val) {
        this.getTableList(this.fetchParams);
      }
      this.isAdd = false;
    },
    // 加载资金类型
    async getAccount() {
      let res = (await getFundsType({ fundsType: 1 })).data.map(x => ({ text: x.accountTypeName, value: x.id }));
      this.topFilterList.find(x => x.fieldName == 'accountTypeId').itemList = res;
    }
  }
};
</script>
